package com.example.pungolo.journal;

import com.example.pungolo.journal.model.Mood;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class MoodSelectionActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mood_selection);

    addOnClickListenerToImageViews();
  }

  private void addOnClickListenerToImageViews() {
    TableLayout table = (TableLayout) findViewById(R.id.moodUI_table);
    View.OnClickListener onClickListener = new MoodOnClickListener();
    int p = 1;
    for (int i = 0; i < table.getChildCount(); i++) {
      View v = table.getChildAt(i);
      if (v instanceof TableRow) {
        TableRow row = (TableRow) v;
        for (int j = 0; j < row.getChildCount(); j++) {
          View vi = row.getChildAt(j);
          if (vi instanceof ImageView) {
            ImageView image = (ImageView) vi;
            Mood m = Mood.getMoodFromId(p++);
            image.setTag(m);
            image.setImageResource(m.drawable);
            image.setContentDescription(getResources().getString(m.description));
            image.setOnClickListener(onClickListener);
          }
        }
      }
    }
  }

  public class MoodOnClickListener implements View.OnClickListener {
    public void onClick(View v) {
      Intent back = new Intent();
      ImageView image = (ImageView)v;
      Mood mood = (Mood)image.getTag();
      back.putExtra("mood_id", mood.id);
      setResult(RESULT_OK, back);
      finish();
    }
  }

}
